package com.thiny.android.game.thunderbolt.role;

import com.thiny.android.game.thunderbolt.util.Constants;
import com.thiny.android.game.thunderbolt.view.GameView;

public class Bullet extends BaseRole {
	private float speedX, speedY;

	public Bullet(GameView gameView, float x, float y, int dir, int type,
			int speed) {
		super(gameView, x, y, dir, type, speed);
		if (type == Constants.ENEMY_BULLET_3) {
			float targetX = mGameView.getPlayer().getX();
			float targetY = mGameView.getPlayer().getY();

			double distance = Math.sqrt((x - targetX) * (x - targetX)
					+ (y - targetY) * (y - targetY));

			speedX = (float) ((x - targetX) * Constants.ENEMY_BULLET_SPEED / distance);
			speedY = (float) ((y - targetY) * Constants.ENEMY_BULLET_SPEED / distance);
		}
	}
	
	// 移动的方法
	@Override
	public void move() {
		if (type == Constants.ENEMY_BULLET_3) {
			x -= speedX;
			y -= speedY;
		} else if (dir == Constants.DIR_DOWN) {// 向下移动
			y += speed;
		} else if (dir == Constants.DIR_LEFT_UP) {// 向左上移动
			x -= speed;
			y -= speed;
		} else if (dir == Constants.DIR_UP) {// 向上移动
			y -= speed;
		} else if (dir == Constants.DIR_RIGHT_UP) {// 右上移动
			x += speed;
			y -= speed;
		} else if (dir == Constants.DIR_LEFT) {// 向左移动
			x -= speed;
		} else if (dir == Constants.DIR_LEFT_DOWN) {// 向左下移动
			x -= speed;
			y += speed;
		} else if (dir == Constants.DIR_RIGHT) {// 向右移动
			x += speed;
		} else if (dir == Constants.DIR_RIGHT_DOWN) {// 右下移动
			x += speed;
			y += speed;
		}
	}

	@Override
	public void setRoleBitmap(int type) {
		switch (type) {

		case Constants.PLAYER_BULLET_1:
			mRoleBitmap = mGameView.playerBullet1;
			break;
		case Constants.PLAYER_BULLET_2:
			mRoleBitmap = mGameView.playerBullet2;
			break;
		case Constants.ENEMY_BULLET_1:
			mRoleBitmap = mGameView.enemyBullet1;
			break;
		case Constants.ENEMY_BULLET_2:
			mRoleBitmap = mGameView.enemyBullet2;
			break;
		case Constants.ENEMY_BULLET_3:
			mRoleBitmap = mGameView.enemyBullet3;
			break;
		}
	}

}
